import 'package:sl_doclever/src/utils/sl_utils.dart';

class SLObjcCode {
  static String property_NSArray(
    String type,
    String name,
    String remark, {
    bool hasSub = false,
  }) {
    type = hasSub ? 'NSArray<$type *>*' : 'NSArray*';
    return '/// $remark\n'
        '@property (nonatomic, strong) $type $name;';
  }

  static String property_NSArray_init(String type, String name) =>
      '_$name = [NSArray array];';

  static String property_NSString(String name, String remark) => '/// $remark\n'
      '@property (nonatomic, copy) NSString* $name;';

  static String property_NSString_init(String name) =>
      '_$name = [NSString string];';

  static String property_NSObject(String type, String name, String remark) =>
      '/// $remark\n'
      '@property (nonatomic, strong) $type* $name;';

  static String property_NSObject_init(String type, String name) =>
      '_$name = [[$type alloc]init];';

  static String modelContainerPropertyGenericClass(List clses) =>
      '+ (NSDictionary *)modelContainerPropertyGenericClass {\n'
      'return @{\n'
      '${clses.join(',')}'
      '};\n'
      '}';

  static String interface(
    String clsName,
    String remark, {
    String author = '',
  }) {
    author = author.isEmpty ? '' : '/// @author $author\n';
    return '/// $remark\n'
        '$author'
        '/// @date ${SLUtils.dateString}\n'
        '@interface $clsName : NSObject';
  }

  static String end() => '@end';

  static String implementation(String clsName) => '@implementation $clsName';

  static String init(List var_list) => '-(instancetype)init{\n'
      'if (self == [super init]) {\n'
      '${var_list.join('\n')}'
      '}\n'
      'return self;\n'
      '}';

  static String interface_sys() => '#import <Foundation/Foundation.h>';
  static String implementation_import(String clsName) => '#import "$clsName"';

  static String interface_singleTon() => '/// 创建单例\n'
      '+(instancetype)sharedManager;\n'
      '/// 销毁单例\n'
      '+(void)destroyResource;';

  static String implementation_singleTon() => 'static id _instance;\n'
      'static dispatch_once_t onceToken;\n'
      '\n'
      '+(instancetype)sharedManager{\n'
      '    dispatch_once(&onceToken, ^{\n'
      '        _instance = [[self alloc] init];\n'
      '    });\n'
      '    return _instance;\n'
      '}\n'
      '\n'
      '+(void)destroyResource{\n'
      '    onceToken = 0;\n'
      '    _instance = nil;\n'
      '}';

  static String implementation_include() =>
      '#if __has_include(<SEGNetworkManager/SEGHTTPBusinessManager.h>)\n'
      '#import <SEGNetworkManager/SEGHTTPBusinessManager.h>\n'
      '#else\n'
      '#import "SEGHTTPBusinessManager.h"\n'
      '#endif\n'
      '\n'
      '#if __has_include(<SEGYYKit/SEGYYModel.h>)\n'
      '#import <SEGYYKit/SEGYYModel.h>\n'
      '#else\n'
      '#import "SEGYYModel.h"\n'
      '#endif';

  static String implementation_property(String clsName, String name) {
    if (clsName.isNotEmpty) {
      return '-($clsName *)$name {\n'
          '    if (!_$name) {\n'
          '        _$name = [[$clsName alloc] init];\n'
          '    }\n'
          '    return _$name;\n'
          '}';
    }
    return '';
  }

  static String implementation_http(String clsName, String name) =>
      'completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {\n\n'
      'if (!isSuccessed || result == nil) {\n'
      '    finished(NO,nil,codeType,error.localizedDescription);\n'
      '    return;\n'
      '}\n\n'
      'if (![result isKindOfClass:[NSDictionary class]]) {\n'
      '    finished(NO,nil,codeType,error.localizedDescription);\n'
      '    return;\n'
      '}\n\n'
      '${clsName.isNotEmpty ? 'self.$name = [$clsName yy_modelWithDictionary:result];\n' : ''}'
      'finished(YES,result,codeType,error.localizedDescription);\n'
      'return;\n'
      '}];';
}
